//
//  RechargeRecordViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/4/27.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "RechargeRecordViewController.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "ChargeTableViewCell.h"
@interface RechargeRecordViewController ()
{
    UIView *backgroundView;
}
@end

@implementation RechargeRecordViewController
@synthesize dateBtn,dateNow,pmCC,array,zTableView;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    [self initNav];
    navTitle.text = @"充值记录";
    [self initUI];
}
#pragma mark - initUI
-(void)initUI{
    backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, navView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT*0.6)];
    backgroundView.backgroundColor = [UIColor whiteColor];
    backgroundView.userInteractionEnabled = YES;
    [self.view addSubview:backgroundView];
    
    //获取现在的日期
    [self strtime];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ijia) name:@"ijia" object:nil];
    
    
    UILabel * dateLabel = [[UILabel alloc] initWithFrame:CGRectMake( 10, 10,70, 20)];
    
    dateLabel.text = @"充值记录";
    dateLabel.textColor = [UIColor blackColor];
    dateLabel.font = [UIFont systemFontOfSize:14];
    [backgroundView addSubview:dateLabel];
    
    
    
    
    dateBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;
    dateBtn.frame = CGRectMake(SCREEN_WIDTH-80,dateLabel.top, 70,25);
    dateBtn.layer.borderColor = [UIColor grayColor].CGColor;
    dateBtn.layer.borderWidth = 0.5;
    //[self.dateBtn setBackgroundImage:[UIImage imageNamed:@"applySelect"] forState:UIControlStateNormal];
    [dateBtn setTintColor:[UIColor grayColor]];
    [dateBtn setTitle:dateNow forState:UIControlStateNormal];
    [dateBtn addTarget:self action:@selector(dateBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView  addSubview:dateBtn];
    
    if (SCREEN_WIDTH == 320) {
        [self createrLabelView:CGRectMake(10, dateLabel.bottom+30, 100, 30) andTitle:@"日期"];
        
        [self createrLabelView:CGRectMake(110, dateLabel.bottom+30, 100, 30) andTitle:@"时间"];
        
        [self createrLabelView:CGRectMake(210, dateLabel.bottom+30, 100, 30) andTitle:@"金额（元）"];
        
        [self createrUI:CGRectMake(110, dateLabel.bottom+30, 1, 30)];
        
        [self createrUI:CGRectMake(210, dateLabel.bottom+30, 1, 30)];
        [self createrUI:CGRectMake(10, dateLabel.bottom+30, SCREEN_WIDTH-20, 1)];
        [self createrUI:CGRectMake(10, dateLabel.bottom+30, 1, 40)];
        [self createrUI:CGRectMake(SCREEN_WIDTH-11, dateLabel.bottom+30, 1, 30)];
    }else if (SCREEN_WIDTH == 375){
        [self createrLabelView:CGRectMake(10, dateLabel.bottom+30, 118, 30) andTitle:@"日期"];
        
        [self createrLabelView:CGRectMake(128, dateLabel.bottom+30, 118, 30) andTitle:@"时间"];
        
        [self createrLabelView:CGRectMake(246, dateLabel.bottom+30, 118, 30) andTitle:@"金额（元）"];
        
        [self createrUI:CGRectMake(128, dateLabel.bottom+30, 1, 30)];
        
        [self createrUI:CGRectMake(246, dateLabel.bottom+30, 1, 30)];
        [self createrUI:CGRectMake(10, dateLabel.bottom+30, SCREEN_WIDTH-20, 1)];
        [self createrUI:CGRectMake(10, dateLabel.bottom+30, 1, 40)];
        [self createrUI:CGRectMake(SCREEN_WIDTH-11, dateLabel.bottom+30, 1, 30)];
    }else{
        [self createrLabelView:CGRectMake(10, dateLabel.bottom+30, 131, 30) andTitle:@"日期"];
        
        [self createrLabelView:CGRectMake(141, dateLabel.bottom+30, 131, 30) andTitle:@"时间"];
        
        [self createrLabelView:CGRectMake(272, dateLabel.bottom+30, 131, 30) andTitle:@"金额（元）"];
        
        [self createrUI:CGRectMake(141, dateLabel.bottom+30, 1, 30)];
        
        [self createrUI:CGRectMake(272, dateLabel.bottom+30, 1, 30)];
        [self createrUI:CGRectMake(10, dateLabel.bottom+30, SCREEN_WIDTH-20, 1)];
        [self createrUI:CGRectMake(10, dateLabel.bottom+30, 1, 40)];
        [self createrUI:CGRectMake(SCREEN_WIDTH-11, dateLabel.bottom+30, 1, 30)];
    }
    //
    self.zTableView = [[UITableView alloc] initWithFrame:CGRectMake(10,dateLabel.bottom+60, self.view.frame.size.width-20, backgroundView.frame.size.height-105)];
    self.zTableView.delegate = self;
    self.zTableView.dataSource = self;
    self.zTableView.bounces = NO;
    self.zTableView.backgroundColor = [UIColor orangeColor];
    [backgroundView addSubview:self.zTableView];
    self.zTableView.backgroundColor = [UIColor clearColor];
    [self.zTableView registerClass:[ChargeTableViewCell class] forCellReuseIdentifier:@"cellID"];
    self.zTableView.separatorColor = [UIColor clearColor];
    [self setExtraCellLineHidden:self.zTableView];
    //
    
}
-(void)ijia{
    i++;
}
#pragma mark - 隐藏多余的分割线
-(void)setExtraCellLineHidden: (UITableView *)tableView

{
    
    UIView *view = [UIView new];
    
    view.backgroundColor = [UIColor clearColor];
    
    [tableView setTableFooterView:view];
    
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChargeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    
    
    cell.date.text = @"2015-03-18";
    cell.time.text = @"16:52";
    cell.money.text = @"￥15000";
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 7;
}
#pragma mark -点击日期
- (void)dateBtnAction:(UIButton *)btn
{
    NSLog(@"日期");
    self.pmCC = [[PMCalendarController alloc] init];
    pmCC.delegate = self;
    pmCC.mondayFirstDayOfWeek = YES;
    
    [pmCC presentCalendarFromView:btn
         permittedArrowDirections:PMCalendarArrowDirectionAny
                         animated:YES];
    [self calendarController:pmCC didChangePeriod:self.pmCC.period];
    
}

static int i = 0;
- (void)calendarController:(PMCalendarController *)calendarController didChangePeriod:(PMPeriod *)newPeriod
{
    self.dateBtn.titleLabel.text = [NSString stringWithFormat:@"%@"
                                    , [newPeriod.startDate dateStringWithFormat:@"yyyy-MM"]];
    
    dateNow =[NSString stringWithFormat:@"%@"
              , [newPeriod.startDate dateStringWithFormat:@"yyyy-MM"]];
    if (i%2!=0) {
        [self.pmCC dismissCalendarAnimated:YES];
        
        //        AccountHandleData *data = [[AccountHandleData alloc] init];
        //        data.delegate = self;
        //        [data topUPRecords:single.buyerModel.user_id andTime:dateNow];
        
    }
    
    i++;
    
    
}


#pragma mark - 返回
-(void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
-(void)createrLabelView:(CGRect)rect andTitle:(NSString *)title{
    
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.text = title;
    label.backgroundColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    [backgroundView addSubview:label];
}

-(void)createrUI:(CGRect)rect{
    UIView *lines = [[UIView alloc] initWithFrame:rect];
    lines.backgroundColor = [UIColor colorWithRed:179/255.0 green:179/255.0 blue:179/255.0 alpha:1];
    [backgroundView addSubview:lines];
}

#pragma mark - 获取当前时间
-(void)strtime{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *now;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    now=[NSDate date];
    comps = [calendar components:unitFlags fromDate:now];
    NSInteger year = [comps year];
    NSInteger month = [comps month];
    NSInteger day = [comps day];
    NSString *monthStr ;
    NSString *dayStr ;
    if (month < 10 || day < 10) {
        if (month <10) {
            monthStr = [NSString stringWithFormat:@"0%ld",(long)month];
        }else{
            monthStr = [NSString stringWithFormat:@"%ld",(long)month];
        }
        if(day < 10){
            dayStr = [NSString stringWithFormat:@"0%ld",(long)day];
        }else{
            dayStr = [NSString stringWithFormat:@"%ld",(long)day];
        }
    }else{
        monthStr = [NSString stringWithFormat:@"%ld",(long)month];
        dayStr = [NSString stringWithFormat:@"%ld",(long)day];
    }
    
    //dateNow = [NSString stringWithFormat:@"%ld-%@-%@",(long)year,monthStr,dayStr];
    dateNow = [NSString stringWithFormat:@"%ld-%@",(long)year,monthStr];
}

@end
